import { Controller, Get, Logger, Query, Render, Req, Res } from '@nestjs/common';
import { Response, Request } from 'express';
import { AppService } from './app.service';
import axios, { Axios, AxiosInstance } from 'axios';
import { Cron, CronExpression } from '@nestjs/schedule';
const https = require('https');

@Controller()
export class AppController {
  requestNet: AxiosInstance;
  constructor(private appService: AppService) {}
  
  @Get('')
  home(@Req() req: Request, @Res() res: Response) {
    return res.render('index', {});
  }
  @Get('index')
  index(@Req() req: Request, @Res() res: Response) {
    return res.render('index', {});
  }

  @Get('header')
  async headerAndFooter(@Req() req: Request, @Res() res: Response) {
    return await this.appService.handleHeaderAndFooter(req, res);
  }

  @Get('center')
  async center(@Req() req: Request, @Res() res: Response) {
    return await this.appService.handleJumpCenter(req, res);
  }

  @Get('editor')
  async root(@Req() req: Request, @Res() res: Response) {
    const fileUrl = req.query.header_file || '';
    if (fileUrl.length) {
      const config = await this.appService.getSelectTempateConfig(req);
      return res.render('editor', { ...config });
    }
    const config = await this.appService.getEditorConfig(req);
    return res.render('editor', { ...config });
  }

  @Get('template')
  async configTemplate(@Req() req: Request, @Res() res: Response) {
    const config = await this.appService.getTempateConfig(req);
    return res.render('editor', { ...config });
  }

  // @Get('saveEditor')
  // async saveEditor(@Req() req: any) {
  //   const baseStr = await this.appService.saveEditor();
  //   const params = { HEADER_ID: '', FORM_VER_ID: req.form_ver_id, FILENAME: 'test2', BASE64STRING: baseStr };
  //   return this.requestNet.post('', params);
  // }
}
